
old1 = read_mrt('./ancillary/Anderson2009_table1.mrt',count=n1a)
old2 = read_mrt('./ancillary/Anderson2009_table2.mrt',count=n1)
new  = read_mrt('./ancillary/Anderson2012_table1.mrt',count=n2)
ari1 = read_mrt('./ancillary/Bania2012_table1.mrt',count=n3a)
ari2 = read_mrt('./ancillary/Bania2012_table2.mrt',count=n3b)
ari5 = read_mrt('./ancillary/Bania2012_table5.mrt',count=n3c)


IF n1 NE n1a THEN print,'HELP!'
IF n3a NE n3b THEN print,'HELP!'
IF n3a NE n3c THEN print,'HELP!'
IF n3b NE n3c THEN print,'HELP!'


ind3 = where_array(ari1.name,ari2.name,ct)
print,ct
print,n3a,n3b,n3c



;; help,old1,/str
;; help,old2,/str
;; help,new,/str
;; help,ari1,/str
print,n_elements(ari1)
;; Make structure
hrds = replicate( {name:'',$
                   glon:0.d,$
                   glat:0.d,$
                   source:0L,$
                   vlsr:0.d,$
                   rgal:0.d,$
                   dnear:0.d,$
                   dfar:0.d,$
                   dsun:0.d,$
                   e_dsun:0.d,$
                   kdar:'',$
                   qf:'',$
                   z:0.d}, n1+n2+n3c)

;; Start filling it in...
;; OLD
kdar = strarr(n1)
qf   = strarr(n1)
print,'-------------------------------------'
FOR ii=0,n1-1 DO BEGIN
   
   force = (strmid(old2[ii].n_f_e_a,1) EQ '*') + $
           (strmid(old2[ii].n_f_sa,1) EQ '*')*2
   
   CASE 1 OF
      force NE 0: BEGIN         ; Catalog chooses a value for us
         IF force EQ 1 THEN BEGIN
            kdar[ii] = strmid(old2[ii].n_f_e_a,0,1)
            qf[ii]   = old2[ii].cea
         ENDIF ELSE BEGIN            
            kdar[ii] = strmid(old2[ii].n_f_sa,0,1)
            qf[ii]   = old2[ii].csa
         ENDELSE      
      END
      
      old2[ii].cea EQ 'A': BEGIN ; Choose CEA = 'A' First
         kdar[ii] = old2[ii].n_f_e_a
         qf[ii]   = old2[ii].cea
         ;; print,'>>>>>>>>'+old2[ii].n_f_e_a+'     '+old2[ii].n_f_sa
      END
      
      old2[ii].csa EQ 'A': BEGIN ; Choose CSA = 'A' Next
         kdar[ii] = old2[ii].n_f_sa
         qf[ii]   = old2[ii].csa
         ;; print,'>>>>>>>>'+old2[ii].n_f_sa+'     '+old2[ii].n_f_e_a
      END
      
       old2[ii].cea EQ 'B': BEGIN ; Choose CEA = 'B' Next Next
         kdar[ii] = old2[ii].n_f_e_a
         qf[ii]   = old2[ii].cea
         ;; print,'>>>>>>>>'+old2[ii].n_f_e_a+'     '+old2[ii].n_f_sa
      END
      
      old2[ii].csa EQ 'B': BEGIN ; Choose CSA = 'B' Last
         kdar[ii] = old2[ii].n_f_sa
         qf[ii]   = old2[ii].csa
         ;; print,'>>>>>>>>'+old2[ii].n_f_sa+'     '+old2[ii].n_f_e_a
      END
      
      old2[ii].n_f_e_a EQ 'T': BEGIN ; As a last resort, Tangent points
         kdar[ii] = old2[ii].n_f_e_a
         qf[ii]   = old2[ii].cea
      END    
            
      ELSE: print,string(old2[ii].csa,old2[ii].n_f_sa,$
                         old2[ii].cea,old2[ii].n_f_e_a,$
                         format="(A3,A3,A3,A3)")
   ENDCASE   
   
   
   
ENDFOR
print,kdar
print,'-------------------------------------'
;; STOP


hrds[0:n1-1].name   = old1.name
hrds[0:n1-1].glon   = old1.glon
hrds[0:n1-1].glat   = old1.glat
hrds[0:n1-1].vlsr   = old1.vlsr
hrds[0:n1-1].rgal   = old1.rgal
hrds[0:n1-1].dnear  = old1.dnear
hrds[0:n1-1].dfar   = old1.dfar
hrds[0:n1-1].dsun   = old2.dsun
hrds[0:n1-1].kdar   = kdar
hrds[0:n1-1].qf     = qf
hrds[0:n1-1].z      = old2.z
hrds[0:n1-1].source = 0

;; OLD
hrds[n1:n1+n2-1].name   = new.name
hrds[n1:n1+n2-1].glon   = new.glon
hrds[n1:n1+n2-1].glat   = new.glat
hrds[n1:n1+n2-1].rgal   = new.rgal
hrds[n1:n1+n2-1].vlsr   = new.vlsr
hrds[n1:n1+n2-1].dnear  = new.dn
hrds[n1:n1+n2-1].dfar   = new.df
hrds[n1:n1+n2-1].dsun   = new.d
hrds[n1:n1+n2-1].e_dsun = new.e_d
hrds[n1:n1+n2-1].kdar   = new.n_f
hrds[n1:n1+n2-1].qf     = new.qf
hrds[n1:n1+n2-1].z      = new.z
hrds[n1:n1+n2-1].source = 1


;;==================================
;; Aricebo

;; Combine ARI into a single structure, then insert into HRDS
ari = replicate( {name:'',$
                  glon:0.d,$
                  glat:0.d,$
                  vlsr:0.d,$
                  rgal:0.d,$
                  dn:0.d,$
                  df:0.d,$
                  d:0.d,$
                  e_d:0.d,$
                  n_f:'',$
                  qf:'',$
                  z:0.d}, n3c)

i15 = where_array(ari5.name,ari1.name,n15)
ari1 = ari1[i15]

print,n_elements(ari1),n_elements(ari5)

ari.name = ari5.name
ari.glon = ari1.glon
ari.glat = ari1.glat
ari.vlsr = ari5.vlsr
ari.rgal = ari5.rgal
ari.dn   = ari5.dn
ari.df   = ari5.df
ari.d    = ari5.dsun
ari.e_d  = ari5.e_dsun
ari.n_f  = ari5.n_f
ari.z    = ari5.z



hrds[n1+n2:*].name   = ari.name
hrds[n1+n2:*].glon   = ari.glon
hrds[n1+n2:*].glat   = ari.glat
hrds[n1+n2:*].vlsr   = ari.vlsr
hrds[n1+n2:*].rgal   = ari.rgal
hrds[n1+n2:*].dnear  = ari.dn
hrds[n1+n2:*].dfar   = ari.df
hrds[n1+n2:*].dsun   = ari.d
hrds[n1+n2:*].e_dsun = ari.e_d
hrds[n1+n2:*].kdar   = ari.n_f
hrds[n1+n2:*].qf     = ari.qf
hrds[n1+n2:*].z      = ari.z
hrds[n1+n2:*].source = 2


help,hrds,/str
print,n_elements(hrds)

hrds = hrds[sort(hrds.glon)]

;; forprint,'>>'+hrds.qf+'<<>>'+hrds.kdar+'<<>>'+$
;;          string(hrds.source,format="(I0)")+'<<'










;; ONLY SAVE IF QF = 'A' or 'B'

ind = where(hrds.qf EQ 'A' OR hrds.qf EQ 'B' OR $
            hrds.kdar EQ 'T' OR hrds.source EQ 2, nab)
print,nab,n_elements(hrds)
hrds = hrds[ind]

save,hrds,filename='./ancillary/HRDS_catalog.sav',/ver

;; Write out PMRT for HRDS catalog


print,m4_stat(hrds.z)

n = n_elements(hrds)

openw,lun,'./ancillary/HRDS_cat.pmrt',/get_lun

FOR ii=0, n-1 DO BEGIN
   
   h = hrds[ii]
   
   z = h.z LE -1d4 ? '---' : string(h.z,format="(F0.1)")
   
   str = string(h.name,h.glon,h.glat,h.source,h.vlsr,h.rgal,h.dnear,h.dfar,$
                h.dsun,h.e_dsun,h.kdar,h.qf,z,format="(A0,' & ',F0.3,' & ',"+$
                "F0.3,' & ',I0,' & ',F0.1,' & ',F0.2,' & ',F0.2,' & ',"+$
                "F0.2,' & ',F0.2,' & ',F0.2,' & ',A0,' & ',A0,' & ',A0)")
   
   ;; Check str for NaN
   WHILE strmatch(str,'*NaN*') DO BEGIN
      
      print,'We Have NaN!'
      
      ;; Replace 'NaN' with '---'
      nanpos = strpos(str,'NaN')
      str = strmid(str,0,nanpos) + '---' + strmid(str,nanpos+3)
      print,'>>'+str+'<<'
      
      
      
      
   ENDWHILE
   
   printf,lun,str
   
ENDFOR

close,lun
free_lun,lun


;; Write out PMRT header info for use with AAS tool thingie
openw,lun,'./ancillary/HRDS_cat.hdr',/get_lun

printf,lun,'---'
printf,lun,'deg'
printf,lun,'deg'
printf,lun,'---'
printf,lun,'km/s'
printf,lun,'kpc'
printf,lun,'kpc'
printf,lun,'kpc'
printf,lun,'kpc'
printf,lun,'kpc'
printf,lun,'---'
printf,lun,'---'
printf,lun,'pc'

printf,lun,''
printf,lun,''
printf,lun,''

printf,lun,'Name'
printf,lun,'GLON'
printf,lun,'GLAT'
printf,lun,'Source'
printf,lun,'VLSR'
printf,lun,'Rgal'
printf,lun,'Dnear'
printf,lun,'Dfar'
printf,lun,'Dsun'
printf,lun,'e_Dsun'
printf,lun,'KDAR'
printf,lun,'QF'
printf,lun,'z'

printf,lun,''
printf,lun,''
printf,lun,''

printf,lun,'Object Name'
printf,lun,'Galactic Longitude'
printf,lun,'Galactic Latitude'
printf,lun,'Paper source (1)'
printf,lun,'LSR Velocity'
printf,lun,'Galactocentric Radius'
printf,lun,'Near Kinematic Distance'
printf,lun,'Far Kinematic Distance'
printf,lun,'Heliocentric Distance'
printf,lun,'standard error on above'
printf,lun,'KDA Resolution'
printf,lun,'Quality Factor (2)'
printf,lun,'Vertical Displacement from Midplane'

close,lun
free_lun,lun

;;******************************************************************************
;;******************************************************************************
;;******************************************************************************
;;******************************************************************************
;;******************************************************************************
;;******************************************************************************
;; Write out PMRT for Paper Table 2


print,m4_stat(hrds.z)

n = n_elements(hrds)

openw,lun,'./grs_paper/table2.pmrt',/get_lun

FOR ii=0, n-1 DO BEGIN
   
   h = hrds[ii]
   
   z = h.z LE -1d4 ? '---' : string(h.z,format="(F0.1)")
   
   str = string(h.name,h.glon,h.glat,h.vlsr,$
                h.dsun,h.kdar,h.qf,h.source+1,format="(A0,' & ',F0.3,' & ',"+$
                "F0.3,' & ',F0.1,' & ',F0.2,' & ',A0,' & ',A0,' & ',I0)")
   
   ;; Check str for NaN
   WHILE strmatch(str,'*NaN*') DO BEGIN
      
      print,'We Have NaN!'
      
      ;; Replace 'NaN' with '---'
      nanpos = strpos(str,'NaN')
      str = strmid(str,0,nanpos) + '---' + strmid(str,nanpos+3)
      print,'>>'+str+'<<'
      
      
      
      
   ENDWHILE
   
   printf,lun,str
   
ENDFOR

close,lun
free_lun,lun


;; Write out PMRT header info for use with AAS tool thingie
openw,lun,'./grs_paper/table2.hdr',/get_lun

printf,lun,'---'
printf,lun,'deg'
printf,lun,'deg'
printf,lun,'km/s'
printf,lun,'kpc'
printf,lun,'---'
printf,lun,'---'
printf,lun,'---'

printf,lun,''
printf,lun,''
printf,lun,''

printf,lun,'Name'
printf,lun,'GLON'
printf,lun,'GLAT'
printf,lun,'VLSR'
printf,lun,'Dsun'
printf,lun,'KDAR'
printf,lun,'QF'
printf,lun,'Ref'

printf,lun,''
printf,lun,''
printf,lun,''

printf,lun,'Object Name'
printf,lun,'Galactic Longitude'
printf,lun,'Galactic Latitude'
printf,lun,'LSR Velocity'
printf,lun,'Heliocentric Distance'
printf,lun,'KDA Resolution'
printf,lun,'Quality Factor (1)'
printf,lun,'Reference (2)'

close,lun
free_lun,lun



END
